MGS Example

Figure: Modified Gram-Schmidt Function
\begin{figure}
\begin{verbatim}
//
// Modified Gram-Schmidt
// Given A (MxN), w...
...:m;k] * r[k;j];
 }
 }
 return << q = q; r = r >>;
};\end{verbatim}

\end{figure}
This example (See Figure [*]) implements a modified Gram-Schmidt algorithm to find an orthonormal basis for the input matrix. There are several important points to recognize in this function.
  1. The function returns a list. mgs returns a list containing the matrices Q and R. The members of the list can be accessed by:
    > aa = rand(4,4)
     aa =
     matrix columns 1 thru 4
          0.7       0.96      0.924      0.148  
         0.95      0.915     0.0882      0.879  
       0.0918      0.441      0.908    0.00543  
        0.902     0.0735      0.362      0.222  
    > x = mgs (aa)
       q            r            
    > x.q
     q =
     matrix columns 1 thru 4
         0.47      0.513      0.261     -0.669  
        0.638      0.243     -0.613      0.396  
       0.0617      0.436      0.642      0.628  
        0.606     -0.698      0.379     0.0378  
    > mgs (aa).q
     q =
     matrix columns 1 thru 4
         0.47      0.513      0.261     -0.669  
        0.638      0.243     -0.613      0.396  
       0.0617      0.436      0.642      0.628  
        0.606     -0.698      0.379     0.0378
    
  2. The function argument is copied. The argument A is copied to the local variable a to avoid destroying the original contents of A (or aa in the callers environment).